<?php

define('BASE_PATH', realpath(dirname(__FILE__)) . '/');

$type = $_GET['type'];

if ($type == 'js') {
	$elements = array(
					"js/global_vars.js",
					"js/global_functions.js",
					"js/google_map.js",
					"js/google_chart.js",
					"js/ajax.js",
					"js/forms.js",
					"js/centers.js");
	$type = 'javascript';
} else if ($type="css") {
	$elements = array("css/global.css");
	$type = 'css';
}

$contents = '';
foreach ($elements as $element) {
	$path = realpath(BASE_PATH . '/' . $element);
	$contents .= "\n\n" . file_get_contents($path);
}

// Send Content-Type
header ("Content-Type: text/" . $type . "; charset=utf-8");

$contents = str_replace( array("\n", "\r", "\t", "  "), array('', '', '', ''), $contents );

// Encoding
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
$deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
// Determine used compression method
$encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none');
// Check for buggy versions of Internet Explorer
if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') && 
	preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
	
	$version = floatval($matches[1]);
	if ($version < 6)
		$encoding = 'none';
	if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1')) 
		$encoding = 'none';
}

if (isset($encoding) && $encoding != 'none') {
	$contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
	header ("Content-Encoding: " . $encoding);
	header ('Content-Length: ' . strlen($contents));
} else {
	header ('Content-Length: ' . strlen($contents));
}

echo $contents;